package com.xiaoq.parking.repository.impl;

import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.parking.entity.ParkingRecordEntity;
import com.xiaoq.parking.repository.IParkingRecordRepository;

@Repository
public class ParkingRecordRepositoryImpl implements IParkingRecordRepository {
	
	@Autowired
	private Datastore dataStore;

	@Override
	public ParkingRecordEntity addParkingRecord(ParkingRecordEntity parkingRecord) {
		dataStore.save(parkingRecord);
		return parkingRecord;
	}

	@Override
	public ParkingRecordEntity updateParkingRecord(ParkingRecordEntity parkingRecord) {
		parkingRecord.setUpdatedAt(System.currentTimeMillis());
		dataStore.save(parkingRecord);
		return parkingRecord;
	}

	@Override
	public boolean deleteParkingRecord(String parkingRecId) {
		ParkingRecordEntity parkingRecord = dataStore.createQuery(ParkingRecordEntity.class)
		.field("_id").equal(parkingRecId).get();
		parkingRecord.setIsParking("10");
		dataStore.save(parkingRecord);
		return true;
	}

	@Override
	public List<ParkingRecordEntity> parkingRecordList(String userId) {
		Query<ParkingRecordEntity> parkingRecQuery = dataStore.createQuery(ParkingRecordEntity.class)
				.field("userId").equal(userId)
				.field("isParking").equal("00");
		return parkingRecQuery.asList();
	}

	@Override
	public ParkingRecordEntity parkingRecordDetail(String id) {
		ParkingRecordEntity parkingRecord = dataStore.createQuery(ParkingRecordEntity.class)
				.field("_id").equal(id).get();
		return parkingRecord;
	}

}
